import axios from "axios"
export default {
    namespaced: true,
    state: {
        tableData: [{
            shopName: '熊猫之家',
            shopAdress: '成都市青羊区',
            shopTell: '12345667790',
            shopInformation: '大熊猫小熊猫'
        }],
        shopList: []
    },
    mutations: {
        getShopsList(state, data) {

            if (data === false) {
                alert("当前用户没有所属门店，请申请新店铺")
            } else {
                console.log(data)
                state.shopList = data.map(v => {
                    switch (v.status) {
                        case 1: {
                            return {
                                ...v,
                                status: "申请中"
                            }
                        }
                        case 2: {
                            return {
                                ...v,
                                status: "已通过"
                            }
                        }
                        default: {
                            return {
                                ...v,
                                status: "未通过"
                            }
                        }
                    }
                })
            }
        }
    },
    actions: {
        //获取所有店铺
        getShops(context) {
            const data = JSON.parse(localStorage.getItem("user"));
            const _id = data._id;
            axios
                .get("/shops", {
                    params: {
                        adminId: _id
                    }
                }) ///apply/getApply
                .then(res => {
                    console.log(res)
                    context.commit("getShopsList", res.data)
                })
        },
        //申请店铺
        addShops(context, newShops) {
            const data = JSON.parse(localStorage.getItem("user"));
            const _id = data._id;
            newShops.adminId = _id;
            console.log(newShops)

            axios
                .post("/shops/addShops", newShops)
                .then(res => {
                    console.log(res.data)
                    if (res.data) {
                        alert("申请已提交")
                        context.dispatch('getShops')
                    }
                })

        },

        //修改店铺信息
        updataShopsByid(data, newShops) {
            console.log(newShops)
            axios
                .post("/shops/updateShopsById", {
                    newShops
                })
                .then(res => {
                    console.log(res.data)
                    if (res.data) {
                        alert("申请已提交")
                    } else {
                        alert("修改失败")
                    }
                })
        },

    }
}
